package hu.diploma.android.serviceimpl;

import hu.diploma.common.service.EmailService;
import hu.diploma.common.vos.EmailVo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

public class EmailServiceImpl implements EmailService {

	private final ContentResolver contentResolver;

	public EmailServiceImpl(ContentResolver contentResolver) {
		super();
		this.contentResolver = contentResolver;
	}

	@Override
	public EmailVo save(EmailVo vo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EmailVo merge(EmailVo vo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EmailVo findById(Serializable searchId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public EmailVo findByPhoneId(Serializable searchId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<EmailVo> loadAll() throws Exception {

		Uri uriSMS = Uri.parse("content://com.android.email.provider/message");
		Cursor c = contentResolver.query(uriSMS, null, null, null, null);

		ArrayList<EmailVo> emailVos = new ArrayList<EmailVo>();
		try {

			while (c.moveToNext()) {

				Integer id = c.getInt(c.getColumnIndex(BaseColumns._ID));
				String displayName = c.getString(c.getColumnIndexOrThrow("displayName"));
				String fromList = c.getString(c.getColumnIndexOrThrow("fromList"));
				String timeStamp = c.getString(c.getColumnIndexOrThrow("timeStamp"));
				String subject = c.getString(c.getColumnIndexOrThrow("subject"));

				EmailVo emailVo = new EmailVo();
				emailVo.setPhoneId(id.longValue());
				emailVo.setName(displayName);
				emailVo.setFrom(fromList.split("")[0]);
				emailVo.setDate(new Date(Long.parseLong(timeStamp)));
				emailVo.setSubject(subject);
				emailVos.add(emailVo);
			}
		} finally {
			c.close();
		}
		return emailVos;
	}

	@Override
	public void delete(EmailVo vo) throws Exception {
		// TODO Auto-generated method stub

	}

}
